package org.flexqueries2.utils.date
{
	import com.adobe.utils.StringUtil;

	public class DateUtils
	{
		public static function formatDate(d:Date, format:String = 'YYYY-MM-DD HH:MI:SS'):String
		{
			if(d == null)
				return "";
			
			var formatStrings:Array = ['YYYY', 'MM', 'DD', 'HH', 'HH24', 'MI', 'SS'];
			
			for(var i:Number=0;i<formatStrings.length;i++)
			{
				if(format.indexOf(formatStrings[i]) > -1)
					format = StringUtil.replace(format, formatStrings[i], extractDatePart(d, formatStrings[i]));
				
			}
			
			return format;
			
		}
		
		private static function extractDatePart(d:Date, formatElement:String):String
		{
			if(formatElement == 'YYYY')
				return d.getFullYear().toString();
			
			if(formatElement == 'MM')
				return StringUtils.leftPad((d.getMonth()+1).toString(), '0', 2);
			
			if(formatElement == 'DD')
				return StringUtils.leftPad(d.getDate().toString(), '0', 2);
			
			if(formatElement == 'HH' || formatElement == 'HH24')
				return StringUtils.leftPad(d.getHours().toString(), '0', 2);
			
			if(formatElement == 'MI')
				return StringUtils.leftPad(d.getMinutes().toString(), '0', 2);
			
			if(formatElement == 'SS')
				return StringUtils.leftPad(d.getSeconds().toString(), '0', 2);
			
			return formatElement;
		}
		
		
		public static function toDate(s:String):Date
		{
			var isDateTime:Boolean = s.indexOf(' ') > -1;
			var out:Date = null;
			if(!isDateTime)
			{
				out = new Date();
				out.fullYear = s.split('-')[0];
				out.month = Number(s.split('-')[1])-1;
				out.date = s.split('-')[2];
				
				out.hours = 0;
				out.minutes = 0;
				out.seconds = 0;
				out.milliseconds = 0;
			}
			
			
			return out;
		}
		
		public static function toDateTime(s:String):Date
		{
			try
			{
				var date_arr:Array = s.split(' ')[0].split('-');
				var time_arr:Array = s.split(' ')[1].split(':');
				
				var out:Date = new Date();
				
				out.fullYear = date_arr[0];
				out.month = Number(date_arr[1])-1;
				out.date = Number(date_arr[2]);
				
				out.hours = time_arr[0];
				out.minutes = time_arr[1];
				out.seconds = time_arr[2];			
				
				return out;
			}
			catch(e:Error)
			{
				return null;
			}
			return null;
		}
		
		public static function humanStringToDateTime(s:String):Date
		{
			var date_arr:Array = s.split(' ')[0].split('.');
			var time_arr:Array = s.split(' ')[1].split(':');
			
			var out:Date = new Date();
			
			out.fullYear = date_arr[2];
			out.month = Number(date_arr[1])-1;
			out.date = Number(date_arr[0]);
			
			out.hours = time_arr[0];
			out.minutes = time_arr[1];
			if(time_arr.length > 2)
				out.seconds = time_arr[2];	
			
			return out;
		}
		
		public static function isDateOrDateTime(s:String):Boolean
		{
			return isDate(s) || isDateTime(s);
		}
		
		public static function isDate(s:String):Boolean
		{
			var y:Number = s.split('-')[0];
			var m:Number = s.split('-')[1];
			var d:Number = s.split('-')[2];
			
			var dt:Date = new Date();
			dt.fullYear = y;
			dt.month = m-1;//mois base 0
			dt.date = d;
			
			return formatDate(dt, 'YYYY-MM-DD') == s;
		}
		
		public static function isDateTime(s:String):Boolean
		{
			var dt:Date = toDateTime(s);
			
			return formatDate(dt) == s;
			
		}
		
		/**
		 * Retourne la date du jour à 00:00:00
		 */ 
		public static function today():Date
		{
			var d:Date = new Date();
			d.milliseconds = 0;
			d.seconds = 0;
			d.minutes = 0;
			d.hours = 0;
			
			return d;
			
		}
		
		public static function lastDayOfMonth(year:Number, monthBase0:Number):Number
		{
			var d:Date = new Date();
			d.fullYear = year;
			
			monthBase0++;
			if(monthBase0 == 12)//Si on était en décembre
				monthBase0 = 0;
			d.month = monthBase0;
			d.date = 1;
			d.date--;
			
			return d.date;
		}
		
		/**
		 * Transforme une durée en secondes en une chaine du type hh:mm:ss
		 */ 
		public static function secondsToTime(secLength:Number):String
		{
			var out:String='';
			
			var h:Number = Math.floor(secLength / 3600);
			secLength -= h*3600;
			var m:Number = Math.floor(secLength / 60);
			secLength -= m*60;
			var s:Number = secLength;
			
			out = StringUtils.leftPad(h.toString(), '0', 2) + ':' + StringUtils.leftPad(m.toString(), '0', 2) + ':' + StringUtils.leftPad(s.toString(), '0', 2);
			
			return out;
		}
		
		/**
		 * Transforme une chaine du type hh:mm:ss en nombre de secondes
		 */ 
		public static function timeToSeconds(time:String):Number
		{
			var sec:Number = 0;
			var arr:Array = time.split(':');
			sec = Number(arr[2]);
			sec += Number(arr[1])*60;
			sec += Number(arr[0])*3600;
			/*var out:String='';
			
			var h:Number = Math.floor(secLength / 3600);
			secLength -= h*3600;
			var m:Number = Math.floor(secLength / 60);
			secLength -= m*60;
			var s:Number = secLength;
			
			out = leftPad(h.toString(), '0', 2) + ':' + leftPad(m.toString(), '0', 2) + ':' + leftPad(s.toString(), '0', 2);
			*/
			return sec;
		}
	}
}